package com.my.utils;

import com.my.Server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.time.LocalDateTime;

/**
 * @author WangLuoXin
 * &#064;Date 2024/9/12  17:42
 **/
public class PrintUtil {
    /**
     * @param code    颜色代号：背景颜色代号(41-46)；前景色代号(31-36)
     * @param n       数字+m：1加粗；3斜体；4下划线
     * @param content 要打印的内容
     */
    private static void print(int code, int n, String level, String content) {
        System.out.format("%s\33[%d;%dm%s\33[0m %n", "", code, n, LocalDateTime.now() + "\t" + level + " [" + Thread.currentThread().getName() + "] " + " ================= " + content);
    }


    public static void info(String content) {
        print(42, 1, "INFO", content);
    }


    public static void error(String content) {
        print(41, 1, "ERROR", content);
    }

    public static void warn(String content) {
        print(43, 1, "WARN", content);
    }

    public static void printPath(String content) {
        print(33, 1, "", content);
    }

    public static void simplePrinting(String content) {
        print(34, 1, "", content);
    }

    public static String printHTML(String fileName) {
        StringBuilder sb = new StringBuilder();
        // 使用ClassLoader读取资源文件
        ClassLoader classLoader = Server.class.getClassLoader();
        try (InputStream inputStream = classLoader.getResourceAsStream(fileName);
             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {

            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
}
